﻿
using CPPEI.Coolzon.Designtime.Metadata;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// OrderSetWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OrderSetWindow : Window
    {
        public ObservableCollection<DisplayflowNode> DisplayflowNodes = new ObservableCollection<DisplayflowNode>();
        public OrderSetWindow()
        {
            InitializeComponent();

            this.Loaded += OrderSetWindow_Loaded;
        }

        void OrderSetWindow_Loaded(object sender, RoutedEventArgs e)
        {
            InitControls();
        }

        private void InitControls()
        {
            foreach (DisplayflowNode dfn in UIContext.Current.CurrentSolution.GetDisplayflow().DisplayflowNodes)
            {
                DisplayflowNodes.Add(dfn);
            }

            this.dgDisplayflow.DataContext = DisplayflowNodes;
        }
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            var res = DisplayflowNodes.Where(p => p.CurrentScene != null && p.NextScene != null);

            if (res.Any())
            {
                MessageBoxResult mbr = MessageBox.Show("确认提交", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (mbr == MessageBoxResult.OK)
                {
                    if (UIContext.Current.SolutionNode != null && UIContext.Current.SolutionNode.NodeStatus == NodeStatus.CheckOut)
                    {
                        SetOrder();
                    }
                    else
                    {
                        List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
                        nodeList.Add(UIContext.Current.SolutionNode);
                        CheckOutThread.Start(nodeList, ReportProgress, Completed);
                    }
                }
            }
        }
        public void Completed(bool HasError, int successed, int total)
        {
            if (HasError)
            {
                MessageBox.Show("方案未签出。");
                return;
            }
            SetOrder();
        }

        public void ReportProgress(Guid id, string name, int current, int total)
        {
        }
        private void SetOrder()
        {
            this.Dispatcher.Invoke(() =>
            {
                Displayflow df = UIContext.Current.CurrentSolution.GetDisplayflow();
                df.DisplayflowNodes.Clear();

                var res = DisplayflowNodes.Where(p => p.CurrentScene != null && p.NextScene != null);

                df.DisplayflowNodes = res.ToList();

                UIContext.Current.CurrentSolution.SaveDisplayflow(df);

                LocalStorage.SaveSolutionFile(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution);

                this.Close();
            });
        }

        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e)
        {
            this.dgDisplayflow.SelectAll();
        }

        /// <summary>
        /// 全不选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e)
        {
            this.dgDisplayflow.UnselectAll();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dgDisplayflow.SelectedItems.Count > 0)
            {
                MessageBoxResult mbr = MessageBox.Show("确认删除", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (mbr == MessageBoxResult.OK)
                {
                    while (dgDisplayflow.SelectedItems.Count > 0)
                    {
                        var item = dgDisplayflow.SelectedItems[0];
                        if (item == CollectionView.NewItemPlaceholder)
                        {
                            dgDisplayflow.SelectedItems.Remove(item);
                        }
                        else
                        {
                            DisplayflowNodes.Remove(item as DisplayflowNode);
                        }
                    }
                }
            }
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            DisplayflowNodes.Add(new DisplayflowNode());
        }
    }
}
